﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections.Generic;

using SBPweb.PROvibe.Client.Framework.Controls;
using SBPweb.PROvibe.Client.Framework.Contracts.Content;

namespace SBPweb.PROvibe.Client.Modules.Default
{

    public partial class MainPage : SmartpartPage
    {

        #region Private fields

        private Random rnd = new Random();

        #endregion

        #region Constructor logic

        public MainPage()
            : base()
        {
            InitializeComponent();
            InitializeControls();
        }

        public MainPage(IContentManager contentManager)
            : base(contentManager)
        {
            InitializeComponent();
            InitializeControls();
        }

        #endregion

        protected override void InitializeControls()
        {
            base.InitializeControls();
            this.Caption = "Main page";
            this.spc1.LoadSmartpart(ContentManager, this);
            this.spc3.ContentChanged += new EventHandler(spc3_ContentChanged);
            this.spc3.LoadCustomElement(ContentManager);
            this.spc4.LoadSmartpart(ContentManager, this);
            this.tb.Text = string.Format("Logged in with user '{0}' from host: [{1}]", ContentManager.Session.Get<string>("userName"), ContentManager.Client.ClientIp);
            BindThemes();
        }

        private void spc3_ContentChanged(object sender, EventArgs e)
        {
            SmartpartContainer sp = (sender as SmartpartContainer);
            Grid grid = (sp.Content as UserControl).FindName("LayoutRoot") as Grid;
            grid.Background = new SolidColorBrush(Colors.Transparent);
        }

        private void BindThemes()
        {
            this.cbThemes.ItemsSource = ContentManager.Themes;
            this.cbThemes.DisplayMemberPath = "Key";

            int i = 0;
            foreach (KeyValuePair<string, ResourceDictionary> kvp in this.cbThemes.ItemsSource)
            {
                if (kvp.Key == ContentManager.Themes.CurrentThemeName)
                {
                    this.cbThemes.SelectedIndex = i;
                    break;
                }
                i++;
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            KeyValuePair<string, ResourceDictionary> kvp = (KeyValuePair<string, ResourceDictionary>)e.AddedItems[0];
            if (ContentManager.Themes.CurrentThemeName != kvp.Key)
            {
                ContentManager.Themes.CurrentThemeName = kvp.Key;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int width = (int)((Application.Current.Host.Content.ActualWidth - 250) / 2);
            int height = (int)((Application.Current.Host.Content.ActualHeight - 30) / 2);
            SmartpartContainer spc = new SmartpartContainer();
            spc.Caption = string.Format("Test {0}", Guid.NewGuid());
            spc.SetValue(Canvas.LeftProperty, (double)rnd.Next(-width, width));
            spc.SetValue(Canvas.TopProperty, (double)rnd.Next(-height, height));
            spc.Width = (double)rnd.Next(200, 300);
            spc.Height = (double)rnd.Next(30, 300);
            spControls.Children.Add(spc);
            spc.ApplyTemplate();
            spc.Content = null;
        }

    }

}